import { unique } from "./unique"
// 交 - 去重
// intersect([1, 2, 3, 4, 5, 6, 7], [3, 4, 5], [3, 4], 3) => [3]
export function intersect(...arrays: any[]) {
  if (arrays.length === 0) return []
  const array0 = arrays[0]
  let result = Array.isArray(array0) ? unique(array0) : [array0]
  for (let i = 1; i < arrays.length; i++) {
    const array = arrays[i]
    if (array == null) return []
    if (Array.isArray(array)) {
      result = result.filter(element => array.indexOf(element) !== -1)
    } else {
      result = result.indexOf(array) === -1 ? [] : [array]
    }
  }

  return result
}
